<?php

session_start();

if(empty($_SESSION["log"])){
	header('Location: login.php');
}

include_once("connexion.php");

$sql_pays = "SELECT * from pays";
$result_pays = mysqli_query($db,$sql_pays) or die('Erreur SQL !'.$sql_pays.'<br>'.mysqli_error($db));

$sql_ville = "SELECT * from ville";
$result_ville = mysqli_query($db,$sql_ville) or die('Erreur SQL !'.$sql_ville.'<br>'.mysqli_error($db));

$sql_type = "SELECT * from type";
$result_type = mysqli_query($db,$sql_type) or die('Erreur SQL !'.$sql_type.'<br>'.mysqli_error($db));

if (isset($_POST['envoyer']))
{
	if (empty($_POST['tarif_semaine']) || empty($_POST['tarif_journee']) || empty($_POST['surface']) || empty($_POST['description']) || empty($_POST['postal']) || empty($_POST['adresse']) || empty($_POST['nbsemaine'])) {
		echo "<script>
					alert('Vous n\'avez pas remplie tout les champs !!');
		</script>";
	}
	else
	{
		if (isset($_SESSION['id_user']))
			$id_user = $_SESSION['id_user'];
		else
			echo "VANIQUERTAMEREJAIPASTONID";
		$statut = mysqli_query($db, "UPDATE  user SET  statut =  '2' WHERE  id=".$id_user);

		$tarif_semaine = $_POST['tarif_semaine'];
		$tarif_journee = $_POST['tarif_journee'];
		$surface = $_POST['surface'];
		$type = $_POST['type'];
		$pays = $_POST['pays'];
		$ville = $_POST['ville'];
		$error="";
		$description = $_POST['description'];
		$code_postal = $_POST['postal'];
		$adresse = $_POST['adresse'];
		$jour_debut = $_POST["jour_debut"];
		$mois_debut = $_POST["mois_debut"];
		$annee_debut = $_POST["annee_debut"];
		$nbsemaine = $_POST["nbsemaine"];

		$jour_fin = $jour_debut;
		$mois_fin = $mois_debut;
		$annee_fin = $annee_debut;

		$nbjour = $nbsemaine * 7;
		
		for ($i=0; $i < $nbjour ; $i++) { 
			if ($mois_fin%2 == 0) 
			{
				if ($jour_fin < 31 ) 
				{
					$jour_fin++;
				}
				else
				{
					$jour_fin = 1;
					$mois_fin++;
				}
			}
			else
			{
				if ($jour_fin < 30 ) 
				{
					$jour_fin++;
				}
				else
				{
					$jour_fin = 1;
					$mois_fin++;
				}
			}
			if ($mois_fin > 12) 
			{
				$mois_fin = 1;
				$annee_fin++;
			}
				
		}

		$debut_location = $annee_debut."-".$mois_debut."-".$jour_debut;
		$fin_location = $annee_fin."-".$mois_fin."-".$jour_fin;


		$query = "INSERT INTO tarif VALUES('', '".$tarif_journee."', '".$tarif_semaine."');";
		$res = mysqli_query($db, $query);
		if ($res == false)
			echo "Erreur dans la Tarification";
		$sql_tarif = "SELECT max(id) from tarif";
		$req_tarif = mysqli_query($db, $sql_tarif);
		$tab_tarif = mysqli_fetch_array($req_tarif);
		$id_tarif = $tab_tarif[0];

		$sql_ville = "SELECT * from ville WHERE ville ='$ville'";
		$req_ville = mysqli_query($db, $sql_ville) or die('Erreur SQL !'.$sql_ville.'<br>'.mysqli_error($db));
		$res_ville = mysqli_num_rows($req_ville);

		if ($res_ville == 0) {

			$query = "INSERT INTO ville VALUES ('', '".$ville."', '".$code_postal."');";
			$res = mysqli_query($db, $query);

			$sql_ville = "SELECT * from ville WHERE ville ='$ville'";
			$req_ville = mysqli_query($db, $sql_ville);
			$tab_ville = mysqli_fetch_array($req_ville);
			$id_ville = $tab_ville['id'];
			if ($res == false)
			echo "Erreur dans l'implementation de la ville";
		}
		else
		{
			$tab_ville = mysqli_fetch_array($req_ville);
			$id_ville = $tab_ville['id'];
		}

		$sql_pays = "SELECT * from pays WHERE pays ='$pays'";
		$req_pays = mysqli_query($db, $sql_pays);
		$res_pays = mysqli_num_rows($req_pays);

		if ($res_pays == 0) {
			$query = "INSERT INTO pays VALUES ('', '".$pays."');";
			$res_pays = mysqli_query($db, $query);

			$sql_pays = "SELECT * from pays WHERE pays ='$pays'";
			$req_pays = mysqli_query($db, $sql_pays);
			$tab_pays = mysqli_fetch_array($req_pays);
			$id_pays = $tab_pays['id'];
			if ($res == false)
			echo "Erreur dans l'implementation de la pays";
		}
		else
		{
			$tab_pays = mysqli_fetch_array($req_pays);
			$id_pays = $tab_pays['id'];
		}

		$sql_type = "SELECT id from type WHERE type ='$type'";
		$req_type = mysqli_query($db, $sql_type);
		$id_type = mysqli_fetch_array($req_type);

		if (isset($_FILES['photo']))
		{
			$arrayImg = array('gif', 'jpg', 'jpeg', 'png', 'PNG');
			if($_FILES['photo']['size'] > 0)
			{
				$uploadFile = "photos/" . utf8_decode(basename($_FILES['photo']['name']));
				$file_extension = pathinfo($uploadFile, PATHINFO_EXTENSION);
				if (!in_array($file_extension, $arrayImg))
					$error = "Problème dans le format de l'image";
				if ($error != "" || !move_uploaded_file($_FILES['photo']['tmp_name'], $uploadFile))
					echo "Erreur lors de l'envoi du fichier";
			}
			else
			{
				$uploadFile = "";
			}
		}
		$query = "INSERT INTO bien VALUES ('', '".$id_user."','".$adresse."','".$id_tarif."',
											'".$description."', '".$surface."', '', '".$id_pays."', '".$id_ville."', '".$id_type['id']."');";
		$res = mysqli_query($db, $query);
		if ($res == false)
			echo "Erreur dans l'implémentation de votre annonce".$query;
		$query = "SELECT max(id) FROM bien";
		$res = mysqli_query($db, $query);
		if ($res == false)
			echo "Erreur dans le choppage de l'id du bien";
		$tab_id = mysqli_fetch_array($res);
		$id_bien = $tab_id[0];
		$query = "INSERT INTO reservation VALUES ('', '".$id_user."', '".$id_bien."', '".$debut_location."', '".$fin_location."', '0');";
		$res = mysqli_query($db, $query);
		if ($res == false)
			echo "Erreur dans l'implementation de la reservation";
		$query = "INSERT INTO photo VALUES ('', '".$id_bien."', '".$uploadFile."')";
		$res = mysqli_query($db, $query);
		if ($res == false)
			echo "Erreur dans l'implementation de la photographie";

	}

	
}
?>

<!DOCTYPE html>
<html>
<head>
	<link rel="stylesheet" type="text/css" href="style.css">
	<meta charset="utf-8">
</head>
<body>
	<div id="conteneur">
		<div id="header"> <?php include_once("header.php"); ?><br/></div>
			<div id="contenu">
				<table border="1px" align="center">
					<form method="post" action="new_annonce.php" ENCTYPE="multipart/form-data">
				<tr>
					<td><p>Tarifications semaine</p></td>
					<td>
						<input type="text" id="tarif" name="tarif_semaine" value=""/>
					</td>
				</tr>
				<tr>
					<td><p>Tarifications journée</p></td>
					<td>
						<input type="text" id="tarif" name="tarif_journee" value=""/>
					</td>
				</tr>
				<tr>
					<td><p>Pays du bien</p></td>
					<td>
						<select name="pays">
						    <?php
								while ($data_pays = mysqli_fetch_array($result_pays)) 
								{
									$pays = $data_pays['pays'];
									echo "qsdqsdqsdq<option value=\"$pays\" >".$pays."</option>";
								}
								?>
						</select>
					</td>
				</tr>
				<tr>
					<td><p>Ville du bien</p></td>
					<td>
						<select name="ville">
						    <?php
								while ($data_ville = mysqli_fetch_array($result_ville)) 
								{
									$ville = $data_ville['ville'];
									echo "qsdqsdqsdq<option value=\"$ville\" >".$ville."</option>";
								}
								?>
						</select>
					</td>				</tr>
				<tr>
					<td><p>Adresse du bien</p></td>
					<td>
						<input type="textarea" id="adresse" name="adresse" value=""/>
					</td>
				</tr>
				<tr>
					<td><p>Code Postal</p></td>
					<td>
						<input type="textarea" id="postal" name="postal" value=""/>
					</td>
				</tr>
				<tr>
					<td><p>Pour combien de semaine</p></td>
					<td>
						<input type="textarea" id="nbsemaine" name="nbsemaine" value=""/>
					</td>
				</tr>
				<tr>
					<td><p>Début de location</p></td>
					<td>
						<select name="jour_debut">
							    <?php for ($jour = 1 ; $jour <= 31 ; $jour++){ ?>
							    <option value="<?php echo $jour ?>"><?php echo $jour; ?></option>
								<?php } ?>  
							</select>
							<select name="mois_debut">
		        				<?php for ($mois = 1 ; $mois <= 12 ; $mois++){ ?>
							    <option value="<?php echo $mois ?>"><?php echo $mois; ?></option>
								<?php } ?>  
							</select>
							<select name="annee_debut">
							    <?php for ($annee = 2014 ; $annee <= 2017 ; $annee++){ ?>
							    <option value="<?php echo $annee ?>"><?php echo $annee; ?></option>
								<?php } ?>  
							</select>
					</td>
				</tr>
					<td><p>Surface habitable</p></td>
					<td>
						<input type="text" id="surface" name="surface" value=""/>
					</td>
				</tr>
				<tr>
					<td><p>Type de bien</p></td>
					<td>
						<select name="type">
						    <?php
								while ($data_type = mysqli_fetch_array($result_type)) 
								{
									$type = $data_type['type'];
									echo "qsdqsdqsdq<option value=\"$type\" >".$type."</option>";
								}
								?>
						</select>
					</td>
				</tr>
				<tr>
					<td><p>Description du bien</p></td>
					<td>
						<textarea id="description" name="description" value=""></textarea>
					</td>
				</tr>
				<tr>
					<td>
						Photos du bien
					</td>
					<td>
						<input type="file" value="" id="photo" name="photo" title=""/>
					</td>
				</tr>
				<tr>
					<td colspan="2" align="center"><input type="submit" id="envoyer" name="envoyer"  value="Envoyer"></td>
				</tr>
				
		</form>
	</table>
</div>
	<div id="footer"> <?php include_once("footer.html"); ?> </div>
</div>
</body>
</html>